package com.burgeon.backend.Controller;

import com.burgeon.backend.SHOPBACKEND.entity.Employee;
import com.burgeon.backend.entity.*;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.service.CouponService;
import com.burgeon.backend.service.UserService;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("vip")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private CouponService couponService;
    @Autowired
    private AccountMapper accountMapper;


    @RequestMapping("retailrecorditem")
    public ResponseResult<ArrayList<RetailRecordItem>> GetRetailRecord(String id,HttpSession session){
        String token = session.getAttribute("token").toString();

        ArrayList<RetailRecordItem> data = userService.getVipRetailItem(id,token);
        return new ResponseResult<ArrayList<RetailRecordItem>>(SUCCESS,data);
    }

    @RequestMapping("retailrecord")
    public ResponseResult<ArrayList<RetailRecord>> GetRetailRecord(HttpSession session){
        String token = session.getAttribute("token").toString();
        String phone = session.getAttribute("phone").toString();
        ArrayList<RetailRecord> data = userService.getVipRetail(phone,token);
        return new ResponseResult<ArrayList<RetailRecord>>(SUCCESS,data);
    }

    @RequestMapping("recharge")
    public ResponseResult<ArrayList<RechargeItem>> GetVIPRecharge(HttpSession session) {
        String token = session.getAttribute("token").toString();
        String phone = session.getAttribute("phone").toString();
        ArrayList<RechargeItem> data =  userService.getVipRechargeItem(phone,token);
        return new ResponseResult<ArrayList<RechargeItem>>(SUCCESS,data);
    }

    @RequestMapping("moduser")
    public ResponseResult<Void> ModUser(String uid,User user,HttpSession session) {
        String account = session.getAttribute("account").toString();
        userService.ModVipData(uid, user,account);
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("moduserdata")
    public ResponseResult<User> ModUserData(String id,HttpSession session) {
        String account = session.getAttribute("account").toString();
        User data = userService.GetModVipData(id, account);
        return new ResponseResult<User>(SUCCESS, data);
    }

    @RequestMapping("viptype")
    public ResponseResult<ArrayList> VipType(String account) {
        //String account = session.getAttribute("account").toString();
        //String account = "mgg";
        ArrayList<String> data = userService.GetVipType(account);
        return new ResponseResult<ArrayList>(SUCCESS,data);
    }

    @RequestMapping("deluser")
    public ResponseResult<Void> DelUser(String id,String code, String account) {

        userService.DelVip(id,code, account);
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("delUserByWX")
    public ResponseResult<Void> delUserByWX(String code, String account) {

        userService.DelVipWX(code, account);
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("registerone")
    public ResponseResult<Void> RegisterOne(String name, String gender, String birthday, HttpSession session) {
        session.setAttribute("r-name", name);
        session.setAttribute("r-gender", gender);
        session.setAttribute("r-birthday", birthday);
        return new ResponseResult<>(SUCCESS);
    }
    @RequestMapping("oldcoupon")
    public ResponseResult<List<Coupon>> GetOldCoupon(HttpSession session) {
        String token = session.getAttribute("token").toString();
        String phone = session.getAttribute("phone").toString();
        List<Coupon> data = couponService.OldFindUserCoupon(phone, token);
        System.err.println(data);
        return new ResponseResult<List<Coupon>>(SUCCESS, data);
    }

    @RequestMapping("coupon")
    public ResponseResult<List<Coupon>> GetCoupon(HttpSession session) {
        String token = session.getAttribute("token").toString();
        String phone = session.getAttribute("phone").toString();
        List<Coupon> data = couponService.FindUserCoupon(phone, token);
        System.err.println(data);
        return new ResponseResult<List<Coupon>>(SUCCESS, data);
    }

    @RequestMapping("getuser")
    public ResponseResult<User> GetUser(HttpSession session) {
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("openid").toString();
        User data = userService.FindUser(openid, token);
        session.setAttribute("uid",data.getId());
        session.setAttribute("phone",data.getPhone());
        return new ResponseResult<User>(SUCCESS, data);
    }

    @RequestMapping("register")
    public ResponseResult<Void> Register(String store, HttpSession session) {
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("openid").toString();
        String phone = session.getAttribute("phone").toString();
        String name = session.getAttribute("r-name").toString();
        String gender = session.getAttribute("r-gender").toString();
        String birthday = session.getAttribute("r-birthday").toString();
        String account = accountMapper.GetAccountForToken(token);
        User user = new User();
        user.setPhone(phone);
        user.setOpenid(openid);
        user.setToken(token);
        user.setName(name);
        user.setGender(gender);
        user.setBirthday(birthday);
        user.setStore(store);
        user.setAccount(account);
        userService.RegisterUser(user);
        session.removeAttribute("r-name");
        session.removeAttribute("r-gender");
        session.removeAttribute("r-birthday");

        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("wxfind")
    public ResponseResult<Map> FindWXUser(String start, String message, String account) {
        HashMap data = userService.FindWXUserData(account, start, message);
        return new ResponseResult<Map>(SUCCESS, data);
    }

    @RequestMapping("wx")
    public ResponseResult<Map> GetWXUser(String start, String account) {
        //String account = session.getAttribute("account").toString();
        HashMap data = userService.GetWXData(account, start);
        return new ResponseResult<Map>(SUCCESS, data);
    }

    @RequestMapping("pos")
    public ResponseResult<Map> GetPosUser(String start, String account) {
       // String account = session.getAttribute("account").toString();
        //String account = "mgg";
        //查询所有会员数据
        HashMap data = userService.GetPOSData(account, start);
        return new ResponseResult<Map>(SUCCESS, data);
    }

    @RequestMapping("posfind")
    public ResponseResult<Map> FindPosUser(String start, String message, String account) {
        //String account = session.getAttribute("account").toString();
        HashMap data = userService.FindPOSUserData(account, start, message);
        return new ResponseResult<Map>(SUCCESS, data);
    }

    /**
     * 获取微信关注总数
     * @param account
     * @return
     */
    @RequestMapping("wxcount")
    public ResponseResult<String> getwxcount(String account) {
        String data = userService.getWxVipCount(account);
        return new ResponseResult<>(SUCCESS, data);
    }

    /**
     * 各种会员统计
     * @param account
     * @return
     */
    @RequestMapping("getVariousUser")
    public ResponseResult<HashMap> getVariousUser(String account) {
        HashMap data = userService.getVariousUser(account);
        return new ResponseResult<>(SUCCESS, data);
    }

    /**
     * 获取卡类型
     * @param account
     * @return
     */
    @RequestMapping("getVIpType")
    public ResponseResult<ArrayList<String>> getVIpType(String account) {
         ArrayList<String> data = userService.GetVipType(account);
        return new ResponseResult<>(SUCCESS, data);
    }

    /**
     * 获取店铺
     * @param account
     * @return
     */
    @RequestMapping("getStore")
    public ResponseResult<ArrayList<CardStore>> getStore(String account) {
        ArrayList<CardStore> data = userService.GetVipStore(account);
        return new ResponseResult<>(SUCCESS, data);
    }

    /**
     * 专属导购
     * @param storeId
     * @param account
     * @return
     */
    @RequestMapping("getEmpByStoreId")
    public ResponseResult<ArrayList<Employee>> getEmpByStoreId(String storeId,String account) {
        ArrayList<Employee> data = userService.GetEmpByStoreId(storeId, account);
        return new ResponseResult<>(SUCCESS, data);
    }

    /**
     * 筛选会员
     * @param account
     * @return
     */
    @RequestMapping("screenUsers")
    public ResponseResult<HashMap> screenUsers(String account,String vipType,String store,String empNO,String storejh,
                                              String birthday,String cStartTime,String cEndTime,int page) {
        HashMap data = userService.FindUserBys(account, vipType, store, empNO, storejh,
               birthday, cStartTime, cEndTime, page);
        return new ResponseResult<>(SUCCESS, data);
    }

}
